package com.studentrecipe.controller;

import com.studentrecipe.dto.CompleteComment;
import com.studentrecipe.mapper.MapperComment;
import com.studentrecipe.pojo.Like;
import com.studentrecipe.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class ControllerComment {

    @Autowired
    private MapperComment mapperComment;

    @RequestMapping("/getComment")
    public Result getComment(Integer recipeId){

        Double cs = mapperComment.getScore(recipeId);
        System.out.println(cs);

        CompleteComment completeComment = new CompleteComment(
                mapperComment.getLike(recipeId),
                mapperComment.getComment(recipeId),
                mapperComment.getScore(recipeId)
        );

        return Result.success(completeComment);
    }

    @RequestMapping("/makeComment")
    public Result makeComment(Integer studentId, Integer recipeId, String content){
        if(mapperComment.makeComment(studentId, recipeId, content)){
            return Result.success("Comment success!");
        }
        return Result.fail("Comment failed!");
    }

    @RequestMapping("/deleteComment")
    public Result deleteComment(Integer id){
        if(mapperComment.deleteComment(id)){
            return Result.success("Delete comment success!");
        }
        return Result.fail("Delete comment failed!");
    }

    @RequestMapping("/changeLike")
    public Result changeLike(Integer studentId, Integer recipeId){
        Like like = mapperComment.getOneLike(studentId, recipeId);
        if(like==null){
            if(mapperComment.makeLike(studentId, recipeId)){
                return Result.success("Like success!");
            }
            return Result.fail("Like failed!");
        }
        else{
            if(mapperComment.changeLike(studentId, recipeId) != 0){
                return Result.success("Change like success!");
            }
            return Result.fail("Change like failed!");
        }
    }

    @RequestMapping("/makeScore")
    public Result makeScore(Integer studentId, Integer recipeId, Integer score){
        Like like = mapperComment.getOneLike(studentId, recipeId);
        if(like==null){
            if(mapperComment.makeScore(studentId, recipeId, score)){
                return Result.success("Score success!");
            }
            return Result.fail("Score failed!");
        }
        else{
            if(mapperComment.updateScore(studentId, recipeId, score) != 0){
                return Result.success("Change score success!");
            }
            return Result.fail("Change score failed!");
        }
    }
}
